import { Component, OnInit, Input, Output, EventEmitter, ElementRef } from '@angular/core';

import { InitService } from 'app/public/serve/init/init.service';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { HintsService } from 'app/public/serve/hints/hints.service';

import { app } from '../application.data';
import { appUrl } from '../application.url';

declare var $: any;

@Component({
  selector: 'application-newly-added',
  templateUrl: './application-newly-added.component.html',
  styleUrls: ['./application-newly-added.component.css']
})
export class ApplicationNewlyAddedComponent implements OnInit {

  @Input() getKeySecret : Array<Object>;
  
  @Output() onSubmit = new EventEmitter<void>();

  grantType   : Array<string> = [];    // 许可类型
  scope       : Array<string> = [];    // 授权范围
  
  hasCheckbox : boolean = true;     // 复选框是否勾选

  constructor(
    public initService: InitService,
    private request: CudTableService,
    private hintsService: HintsService
  ) {}

  ngOnInit() {
  }

  /**
   * 默认勾选许可类型
   */ 
  _isGrantType() {
    app.addInit[2].authType.forEach((obj) => {
      this.grantType.push(obj.sysValue);
    });
  }

  /**
   * 许可类型
   * @param val 
   * @param bool 
   */ 
  _grantType(val:string, bool:boolean) { 
    if(bool) {
      this.grantType.push(val);
    }
    else {
      this._remove(this.grantType, val);
    } 
    this._check(this.grantType.length, this.scope.length);
  }

  /**
   * 授权范围
   */ 
  _scope(val:string, bool:boolean) {  
    if(bool) {
      this.scope.push(val);
    }
    else {
      this._remove(this.scope, val);
    }
    this._check(this.grantType.length, this.scope.length);
  }

  /**
   * 反选
   * @param arr 
   * @param val 
   */ 
  private _remove(arr, val) {
    for(let i in arr) {
      if(arr[i]==val) {
        arr.splice(i,1);
      }
    }
  }

  /**
   * 校验是否勾选复选框
   * @param len1
   * @param len2 
   */ 
  private _check(len1, len2) {
    if(len1>0&&len2>0) {
      this.hasCheckbox = false;
    }
    else {
      this.hasCheckbox = true;
    }
  }

  /**
   * 清空数组
   */
  private _empty() {
    this.grantType = [];
    this.scope = [];
  }

  /************************ 新增 ************************/

  /**
   * 新增
   * @param val 
   */ 
  _onSubmit(val) {

      let getRes;
      let formData = new FormData();

      formData.append('parentId',      app.tree.parentId);
      formData.append('institutionId', app.tree.institutionId);
      formData.append('relationsId',   app.tree.relationsId);

      for(let key in val) {
        formData.append(key, val[key]);
      }

      formData.append('target',    'sys/app/'+val.appKey.substr(-8));
      formData.append('grantType', this.grantType.toString()||'');
      formData.append('scope',     this.scope.toString()||'');
      formData.append('imageinfo', $('#picID')[0].files[0]||'');

      $.ajax({
          url          : appUrl.add,
          type         : 'POST',
          data         : formData,
          async        : false,
          cache        : true,
          contentType  : false,
          processData  : false,
          success: function (res) {
              getRes = res;
          },
          error: function (err) {
              getRes = { message: 'error', result_code: 'error' };
          }
      });
      if (getRes.result_code == '0000') {
        this.onSubmit.emit();
      }
      this.hintsService._operationHints(getRes.message, getRes.result_code);
      this.hasCheckbox = true; 
      this._empty();
      this.initService._init();
      $('#newlyAdded').modal('hide');      
      return
  } 

}
